package com.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.annotation.AuthCheck;
import com.study.common.BaseResponse;
import com.study.common.DeleteRequest;
import com.study.common.ResultUtils;
import com.study.constant.UserConstant;
import com.study.exception.ErrorCode;
import com.study.exception.ThrowUtils;
import com.study.model.dto.favoritePost.FavoritePostAddRequest;
import com.study.model.dto.favoritePost.FavoritePostQueryRequest;
import com.study.model.dto.post.PostAddRequest;
import com.study.model.dto.post.PostQueryRequest;
import com.study.model.dto.post.PostUpdateRequest;
import com.study.model.entity.FavoritePost;
import com.study.model.entity.Post;
import com.study.model.vo.FavoritePostVO;
import com.study.model.vo.PostVO;
import com.study.service.FavoritePostService;
import com.study.service.PostService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/favoritePost")
public class FavoritePostController {

    @Resource
    private FavoritePostService favoritePostService;


    /**
     * 创建收藏帖子
     */
    @PostMapping("/add")
    public BaseResponse<Long> addPost(@RequestBody FavoritePostAddRequest favoritePostAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(favoritePostAddRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(favoritePostService.createFavoritePost(favoritePostAddRequest,request));
    }



    /**
     * 根据 id 获取包装类
     */
    @GetMapping("/get/vo")
    public BaseResponse<FavoritePostVO> getFavoritePostVOById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        FavoritePostVO favoritePostVO=favoritePostService.getFavoritePostVOById(id);
        return ResultUtils.success(favoritePostVO);
    }


    /**
     * 删除帖子
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deletePost(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(favoritePostService.removeById(deleteRequest.getId()));

    }



    /**
     * 分页获取帖子封装列表（仅管理员）
     *
     * @param favoritePostQueryRequest 查询请求参数
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<FavoritePostVO>> listFavoritePostVOByPage(@RequestBody FavoritePostQueryRequest favoritePostQueryRequest) {
        ThrowUtils.throwIf(favoritePostQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(favoritePostService.listFavoritePostVOByPage(favoritePostQueryRequest));
    }
}
